//
//  XCPlayViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/10.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCPlayViewController.h"
#import "UIImageView+WebCache.h"
#import "OnlineAVplay.h"
#import "XCSingleBottumView.h"
#import "WDGDatabaseTool.h"
#import "UMSocial.h"
#import "XCUnderSingerTableViewCell.h"
@interface XCPlayViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) BOOL isLike;

@property (nonatomic, assign) BOOL isRotation;

@property (nonatomic, assign) BOOL isSongList;

@property (nonatomic, assign) BOOL isRow;

@property (nonatomic, retain) UIButton *likeButton;

@property (nonatomic, retain) UIButton *playerButton;

@property (nonatomic, retain) UISlider *slider;

@property (nonatomic, retain) NSString *showTime;

@property (nonatomic, retain) NSString *showTime1;

@property (nonatomic, retain) UIButton *backButton;

@property (nonatomic, retain) UIButton *enjoyButton;

@property (nonatomic, retain) WDGDatabaseTool *myLove;

@property (nonatomic, retain) UILabel *leftLable;

@property (nonatomic, retain) UILabel *rightLable;

@property (nonatomic, retain) UILabel *lableSongName;

@property (nonatomic, retain) UILabel *lableSingerName;

@property (nonatomic, assign) PlayWay playWay;

@property (nonatomic, retain) UIButton *xunHuanButton;

@property (nonatomic, assign) NSInteger random;

@property (nonatomic, retain) UIView *songListView;

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation XCPlayViewController

+ (instancetype) sharePlayViewController  {
    static XCPlayViewController *playViewController = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playViewController = [[XCPlayViewController alloc] init];
    });
    return playViewController;
}


- (void)dealloc {
    
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_backGroundimageView release];
    [_playViewHeadImageView release];
    [_songListView release];
    
    [_likeButton release];
    [_slider release];
    [_showTime release];
    [_showTime1 release];
    [_tableView release];
    
    [_muatbleArraySingerName release];
    [_mutableArraySongName release];
    [_mutableHeadIVPicurl release];
    [_nameArray release];
    
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated{
    
    if ([OnlineAVplay shareAVPlayer].isPlay) {
        //[[OnlineAVplay shareAVPlayer] play];
        
        [self.playerButton setBackgroundImage:[[UIImage imageNamed:@"Image-stoper"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//        旋转
        self.isRotation = YES;
        CABasicAnimation *basicAni = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        basicAni.fromValue = [NSNumber numberWithInt:0];
        basicAni.toValue = [NSNumber numberWithInt:M_PI * 4];
        basicAni.duration = 10;
        basicAni.repeatCount = NSIntegerMax;
        basicAni.autoreverses = NO;
        basicAni.cumulative = YES;
        
        [self.playViewHeadImageView.layer addAnimation:basicAni forKey:@"basicAnimation"];
        
        
        
    } else {
        //[[OnlineAVplay shareAVPlayer] pause];
        [_playerButton setBackgroundImage:[[UIImage imageNamed:@"Image-player"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        CFTimeInterval stopTime = [self.playViewHeadImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        self.playViewHeadImageView.layer.speed = 0;
        self.playViewHeadImageView.layer.timeOffset = stopTime;
        
        self.isRotation = NO;
    }
    
    [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    
    self.lableSingerName.text = self.muatbleArraySingerName[_selectIndex];
    self.lableSongName.text = self.nameArray[_selectIndex];
    
    [XCSingleBottumView shareBottomViewBuild].hidden = YES;
#pragma mark - 最近播放
//    [self recentPlay];
#pragma mark - 喜爱
    [self likebuttonInit];

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self recentPlay];

#pragma mark - 播放列表默认隐藏
    self.isSongList = NO;
    
#pragma mark - 循环按钮
    self.playWay = listPlay;
   
#pragma mark - 喜爱
    //        创建表
    [WDGDatabaseTool creatTableWithSQL:@"CREATE TABLE if not exists myLove (id integer PRIMARY KEY AUTOINCREMENT,songName text NOT NULL,singerName text, picUrl text, url text)"];
    //        根据表名获取一个对象
    
    self.myLove = [WDGDatabaseTool DBManageWithTableName:@"myLove"];
#pragma mark - backGroundImageView;
    self.backGroundimageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backGroundimageView.userInteractionEnabled = YES;
    [self.view addSubview:_backGroundimageView];

#pragma mark - 毛玻璃
//  创建需要的毛玻璃特效类型
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//  毛玻璃view 视图
    
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//添加到要有毛玻璃特效的控件中
    
    effectView.frame = _backGroundimageView.bounds;
    [_backGroundimageView addSubview:effectView];
//设置模糊透明度
    effectView.alpha = .9f;
//    
    self.playViewHeadImageView = [[UIImageView alloc] initWithFrame:CGRectMake((wide - high / 3) / 2, high / 8, high / 3, high / 3)];
    
    [_playViewHeadImageView.layer setCornerRadius:high / 6];
    _playViewHeadImageView.layer.masksToBounds = YES;
    _playViewHeadImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.playViewHeadImageView.userInteractionEnabled = YES;
//    然后再给图层添加一个有色的边框
    _playViewHeadImageView.layer.borderWidth = 2;
    _playViewHeadImageView.layer.borderColor = [[UIColor colorWithRed:0.127 green:0.929 blue:0.685 alpha:1.000] CGColor];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self.playViewHeadImageView addGestureRecognizer:tap];
    [self.view addSubview:_playViewHeadImageView];
    [_playViewHeadImageView release];
    [_backGroundimageView release];
    [effectView release];

#pragma mark - 返回的按钮
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.backButton.frame = CGRectMake(20 , 25, 30, 30);
    [self.backButton setImage:[[UIImage imageNamed:@"Image-back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [self.backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backButton];
    self.enjoyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.enjoyButton.frame = CGRectMake(wide - 50, 25, 30, 30);
    [self.enjoyButton setImage:[[UIImage imageNamed:@"Image-enjoy"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
    
    [self.enjoyButton addTarget:self action:@selector(enjoyAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
//    [self.view addSubview:self.backButton];
    [self.view addSubview:self.enjoyButton];
    
#pragma mark - 播放暂停各路按钮
    
    self.playerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _playerButton.frame = CGRectMake(wide * 4 / 9 + 10, high * 6 / 7 - 15, wide / 10, wide / 10);
    [_playerButton addTarget:self action:@selector(playerButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_playerButton];
    
    
    UIButton *lastButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [lastButton setImage:[[UIImage imageNamed:@"Image-last"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    lastButton.frame = CGRectMake(wide * 2 / 9 + 10, high * 6 / 7 - 15, wide / 10, wide / 10);
    [lastButton addTarget:self action:@selector(lastSongButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:lastButton];
    
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [nextButton setImage:[[UIImage imageNamed:@"Image-next"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    nextButton.frame = CGRectMake(wide * 6 / 9 + 10, high * 6 / 7 - 15, wide / 10, wide / 10);
    [nextButton addTarget:self action:@selector(nextSongButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:nextButton];
    [self changePlayButton];
#pragma mark - 列表按钮 
    
    UIButton *playListButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [playListButton setImage:[[UIImage imageNamed:@"Image-playList"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    playListButton.frame = CGRectMake(wide * 8 / 9 - 10, high * 6 / 7 - 15, wide / 10, wide / 10);
    [playListButton addTarget:self action:@selector(playViewEditAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playListButton];
#pragma mark - 播放模式按钮
    self.xunHuanButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.xunHuanButton.frame = CGRectMake(10, high * 6 / 7 - 15, wide / 10 - 5, wide / 10 - 5);
    [self.xunHuanButton addTarget:self action:@selector(xunHuanStyleButton) forControlEvents:UIControlEventTouchUpInside];
    [self.xunHuanButton setBackgroundImage:[UIImage imageNamed:@"Image-listxunhuan"] forState:UIControlStateNormal];
    [self.view addSubview:self.xunHuanButton];
#pragma mark - uislider
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(wide / 9 + 20, high * 5/ 7 + 50, wide * 6 / 9 + 20, 21)];
    [self.slider setThumbImage:[UIImage imageNamed:@"Image-slider"] forState:UIControlStateNormal];
    
    self.slider.minimumValue = 0.0f;
    self.slider.maximumValue = 1.0f;
    
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
    
    [self.view addSubview:self.slider];
    [_slider release];
//    nstimer定时器执行方法
    
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeSlieder:) userInfo:nil repeats:YES];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

    self.leftLable = [[UILabel alloc] initWithFrame:CGRectMake(self.slider.frame.origin.x - 30, high * 5/ 7 + 50, 25, 21)];
    _leftLable.font = [UIFont systemFontOfSize:8];
    _leftLable.text = self.showTime1;
    _leftLable.textColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    [self.view addSubview:_leftLable];
    [_leftLable release];
    
    
    self.rightLable = [[UILabel alloc] initWithFrame:CGRectMake(self.slider.frame.origin.x + self.slider.frame.size.width + 5, high * 5 / 7 + 50, 25, 21)];
    _rightLable.font = [UIFont systemFontOfSize:8];
    _rightLable.text = self.showTime;
    _rightLable.textColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    [self.view addSubview:_rightLable];
    [_rightLable release];

#pragma mark - 歌曲和歌手名字
    self.lableSongName = [[UILabel alloc] initWithFrame:CGRectMake(wide / 3, high / 2 , wide / 3, 21)];
    
    [self.view addSubview:self.lableSongName];
    self.lableSongName.textAlignment = NSTextAlignmentCenter;
    
    
    self.lableSingerName = [[UILabel alloc] initWithFrame:CGRectMake(wide / 3, self.lableSongName.frame.origin.y + self.lableSongName.frame.size.height + 10, wide / 3, 21)];
    [self.view addSubview:self.lableSingerName];
    self.lableSingerName.textAlignment = NSTextAlignmentCenter;
    
    self.lableSingerName.textColor = [UIColor whiteColor];
    self.lableSongName.textColor = [UIColor whiteColor];
    
    
    [_lableSingerName release];
    [_lableSongName release];

    
    
}
#pragma mark - 最近播放
//- (void)recentPlay {
//    //
//    //        创建表
//    [WDGDatabaseTool creatTableWithSQL:@"CREATE TABLE if not exists recentPlay (id integer PRIMARY KEY AUTOINCREMENT,songName text NOT NULL,singerName text, picUrl text, url text)"];
//    //        根据表名获取一个对象
//    
//    WDGDatabaseTool *recentPlay = [WDGDatabaseTool DBManageWithTableName:@"recentPlay"];
//    
//    [recentPlay openDatabase];
//    
//    //        用字典插入一行数据
//    [recentPlay insertDataWithDictionary:@{@"songname": self.nameArray[_selectIndex], @"singername": self.muatbleArraySingerName[_selectIndex], @"picurl":self.mutableHeadIVPicurl[_selectIndex], @"url":self.mutableArraySongName[_selectIndex]}];
//    
//
//    [recentPlay closeDatabase];
//}
#pragma mark - likebutton
- (void)likebuttonInit {
   
    [self.myLove openDatabase];
    
    WDGWhereCondition *selectCondition = [WDGWhereCondition conditionWithColumnName:@"songname" Operator:@"=" Value:self.nameArray[_selectIndex]];
    
    if ([self.myLove selectDataWithWhereCondition:selectCondition].count == 0) {
        
        self.likeButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.likeButton.frame = CGRectMake(wide * 4 / 9 + 10, high * 5 / 7 - 15, wide / 10, wide / 10);
        
        
        [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-heart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.likeButton addTarget:self action:@selector(likeButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_likeButton];
        
        self.isLike = NO;
        
    } else {
        
        self.likeButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.likeButton.frame = CGRectMake(wide * 4 / 9 + 10, high * 5 / 7 - 15, wide / 10, wide / 10);
        
        [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-redHeart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.likeButton addTarget:self action:@selector(likeButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_likeButton];
        
        self.isLike = YES;
        
    }

}


#pragma mark - like
- (void)likeButtonAction {
    
    if (!self.isLike) {
        //        打开数据
        [self.myLove openDatabase];
        //        用字典插入一行数据
        [self.myLove insertDataWithDictionary:@{@"songname": self.nameArray[_selectIndex], @"singername": self.muatbleArraySingerName[_selectIndex], @"picurl":self.mutableHeadIVPicurl[_selectIndex], @"url":self.mutableArraySongName[_selectIndex]}];
                self.isLike = YES;
        [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-redHeart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//        [self.myLove closeDatabase];
    } else {
        
        [self.myLove openDatabase];
        WDGWhereCondition *deleteCondition = [WDGWhereCondition conditionWithColumnName:@"songname" Operator:@"=" Value:self.nameArray[_selectIndex]];
//        根据条件删除
                if ([self.myLove deleteDataWithWhereCondition:deleteCondition]) {
                    NSLog(@"删除成功");
                } else {
                    NSLog(@"删除失败");
                }

        [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-heart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        self.isLike = NO;
    }

    [self.myLove closeDatabase];
    
    
    
}
#pragma mark - 旋转
- (void)handleTap:(UITapGestureRecognizer *)tap {
    if (self.isRotation == NO) {
        
        CFTimeInterval stopTime = [self.playViewHeadImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        self.playViewHeadImageView.layer.speed = 0;
        self.playViewHeadImageView.layer.timeOffset = stopTime;
    } else {
        
        CFTimeInterval stopTime = self.playViewHeadImageView.layer.timeOffset;
        self.playViewHeadImageView.layer.speed = 1.0;
        self.playViewHeadImageView.layer.timeOffset = 0;
        self.playViewHeadImageView.layer.beginTime = 0;
        self.playViewHeadImageView.layer.beginTime = [self.playViewHeadImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
    }
    
    self.isRotation = !self.isRotation;
    
}
#pragma mark - slider
- (void)sliderAction:(UISlider *)slider {
//    跳转到的事件
//    每秒多少帧(fps)
    if (self.slider.value == 1) {
        
        self.slider.value = 0;
        
    } else {
        [[OnlineAVplay shareAVPlayer] seekToTime:CMTimeMakeWithSeconds([OnlineAVplay shareAVPlayer].currentItem.duration.value / [OnlineAVplay shareAVPlayer].currentItem.duration.timescale * self.slider.value, [OnlineAVplay shareAVPlayer].currentTime.timescale) completionHandler:^(BOOL finished) {
            
        }];
    }
    
}
// 歌曲改变 slider
- (void)changeSlieder:(NSTimer *)sender {
    if (_playWay == listPlay) {
        if (self.slider.value == 1) {
            
            if (_selectIndex != self.mutableArraySongName.count - 1) {
                
                _selectIndex++;
                
                [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [self.selectIndex]];
                [[OnlineAVplay shareAVPlayer] play];
                [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
                [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
                
                [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
                self.lableSingerName.text = self.muatbleArraySingerName[self.selectIndex];
                self.lableSongName.text = self.nameArray[self.selectIndex];
                
            } else {
                
                [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [0]];
                [[OnlineAVplay shareAVPlayer] play];
                
                [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [0]];
                [[OnlineAVplay shareAVPlayer] play];
                
                [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[0]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
                
                [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[0]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
                [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[0]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
                self.lableSingerName.text = self.muatbleArraySingerName[0];
                self.lableSongName.text = self.nameArray[0];
                
            }
            
        }

    } else if (_playWay == singlePlay) {
        if (self.slider.value == 1) {
            self.slider.value = 0;
            [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [self.selectIndex]];
            [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
            [[OnlineAVplay shareAVPlayer] play];
        }

    } else {
        if (self.slider.value == 1) {
              
                self.random = arc4random() % (self.mutableArraySongName.count - 1);
                
                [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [_random]];
            
                [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[_random]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
                [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[_random]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
               self.lableSingerName.text = self.muatbleArraySingerName[_random];
               self.lableSongName.text = self.nameArray[_random];
                [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[_random]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
                [[OnlineAVplay shareAVPlayer] play];
        }

    }
    
        if ([OnlineAVplay shareAVPlayer].currentItem.duration.value) {
        self.slider.value = CMTimeGetSeconds([OnlineAVplay shareAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([OnlineAVplay shareAVPlayer].currentItem.duration);
        }
}
#pragma mark - share
- (void)enjoyAction:(id) sender {
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010" shareText:@"你要分享的文字" shareImage:[UIImage imageNamed:@"Image-p"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToTencent,nil] delegate:nil];
}
#pragma mark - back
- (void)backAction:(id) sender {
    if (_playWay == randomPlay) {
        [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[_random]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        [XCSingleBottumView shareBottomViewBuild].singerNameLable.text = self.muatbleArraySingerName[_random];
        [XCSingleBottumView shareBottomViewBuild].songNameLable.text = self.nameArray[_random];
    } else {
        
        [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        [XCSingleBottumView shareBottomViewBuild].singerNameLable.text = self.muatbleArraySingerName[self.selectIndex];
        [XCSingleBottumView shareBottomViewBuild].songNameLable.text = self.nameArray[self.selectIndex];
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];

}
#pragma mark - play
- (void)playerButtonAction{

    if ([OnlineAVplay shareAVPlayer].isPlay) {
        [[OnlineAVplay shareAVPlayer] pause];
        [self.playerButton setBackgroundImage:[[UIImage imageNamed:@"Image-player"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//        旋转停止
        CFTimeInterval stopTime = [self.playViewHeadImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        self.playViewHeadImageView.layer.speed = 0;
        self.playViewHeadImageView.layer.timeOffset = stopTime;
        
        [OnlineAVplay shareAVPlayer].isPlay = NO;
    } else {
        [[OnlineAVplay shareAVPlayer] play];
        [self.playerButton setBackgroundImage:[[UIImage imageNamed:@"Image-stoper"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//        旋转继续
        CFTimeInterval stopTime = self.playViewHeadImageView.layer.timeOffset;
        self.playViewHeadImageView.layer.speed = 1.0;
        self.playViewHeadImageView.layer.timeOffset = 0;
        self.playViewHeadImageView.layer.beginTime = 0;
        self.playViewHeadImageView.layer.beginTime = [self.playViewHeadImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
        [OnlineAVplay shareAVPlayer].isPlay = YES;

    }
    
}

- (void)nextSongButtonAction {
    if (_selectIndex != self.nameArray.count - 1) {
        [self.myLove openDatabase];
        WDGWhereCondition *selectCondition = [WDGWhereCondition conditionWithColumnName:@"songname" Operator:@"=" Value:self.nameArray[_selectIndex + 1]];
        [self.myLove openDatabase];
        
        if ([self.myLove selectDataWithWhereCondition:selectCondition].count == 0) {
            
            [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-heart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            self.isLike = NO;
            
        } else {
            
            [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-redHeart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            
            self.isLike = YES;
            
        }
        [self.myLove closeDatabase];
        
    } else {
        [self.myLove openDatabase];
        WDGWhereCondition *selectCondition = [WDGWhereCondition conditionWithColumnName:@"songname" Operator:@"=" Value:self.nameArray[0]];
        [self.myLove openDatabase];
        
        if ([self.myLove selectDataWithWhereCondition:selectCondition].count == 0) {
            
            [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-heart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            self.isLike = NO;
            
        } else {
            
            [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-redHeart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            
            self.isLike = YES;
            
        }
        [self.myLove closeDatabase];
    }
    
    
    if (_selectIndex != self.mutableArraySongName.count - 1) {
        [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [self.selectIndex + 1]];
        [[OnlineAVplay shareAVPlayer] play];
        
        [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex + 1]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex + 1]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex + 1]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        self.lableSingerName.text = self.muatbleArraySingerName[_selectIndex + 1];
        self.lableSongName.text = self.nameArray[_selectIndex + 1];
        
        _selectIndex++;
    } else {
        _selectIndex = 0;
        [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [0]];
        [[OnlineAVplay shareAVPlayer] play];
        
        [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[0]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[0]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [[XCSingleBottumView alloc].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[0]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        self.lableSingerName.text = self.muatbleArraySingerName[0];
        self.lableSongName.text = self.nameArray[0];
        
        
    }
    
}
- (void)lastSongButtonAction {
    
    if (_selectIndex != 0) {
        [self.myLove openDatabase];
        WDGWhereCondition *selectCondition = [WDGWhereCondition conditionWithColumnName:@"songname" Operator:@"=" Value:self.nameArray[_selectIndex - 1]];
        [self.myLove openDatabase];
        
        if ([self.myLove selectDataWithWhereCondition:selectCondition].count == 0) {
            
            [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-heart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            self.isLike = NO;
            
        } else {
            
            [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-redHeart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            
            self.isLike = YES;
            
        }
        [self.myLove closeDatabase];
        
    } else {
        [self.myLove openDatabase];
        WDGWhereCondition *selectCondition = [WDGWhereCondition conditionWithColumnName:@"songname" Operator:@"=" Value:self.nameArray[self.mutableArraySongName.count - 1]];
        [self.myLove openDatabase];
        
        if ([self.myLove selectDataWithWhereCondition:selectCondition].count == 0) {
            
            [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-heart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            self.isLike = NO;
            
        } else {
            
            [self.likeButton setBackgroundImage:[[UIImage imageNamed:@"Image-redHeart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            
            self.isLike = YES;
            
        }
        [self.myLove closeDatabase];
    }

    
    if (_selectIndex == 0) {
        _selectIndex = self.mutableArraySongName.count - 1;
        [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [_selectIndex]];
        [[OnlineAVplay shareAVPlayer] play];
        
        [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [[XCSingleBottumView alloc].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        self.lableSingerName.text = self.muatbleArraySingerName[_selectIndex];
        self.lableSongName.text = self.nameArray[_selectIndex];

    } else {
        _selectIndex--;
        [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName [self.selectIndex]];
        [[OnlineAVplay shareAVPlayer] play];
        
        [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [[XCSingleBottumView alloc].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
        self.lableSingerName.text = self.muatbleArraySingerName[_selectIndex];
        self.lableSongName.text = self.nameArray[_selectIndex];
        
    }
}
- (void)changePlayButton {
    
    if ([OnlineAVplay shareAVPlayer].isPlay) {
        [_playerButton setBackgroundImage:[[UIImage imageNamed:@"Image-stoper"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    } else {
        [_playerButton setBackgroundImage:[[UIImage imageNamed:@"Image-player"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
}
// 隐藏 bottomView
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [XCSingleBottumView shareBottomViewBuild].hidden = NO;
    if ([OnlineAVplay shareAVPlayer].isPlay) {
        [[XCSingleBottumView shareBottomViewBuild].buttonPlay setBackgroundImage:[[UIImage imageNamed:@"Image-bottumStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    } else {
        [[XCSingleBottumView shareBottomViewBuild].buttonPlay setBackgroundImage:[[UIImage imageNamed:@"Image-bottumPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    
}
#pragma mark - 时间
- (void)timeAction {
    
    CMTime presentTime = [OnlineAVplay shareAVPlayer].currentItem.currentTime;
    
    CMTime totalTime = [OnlineAVplay shareAVPlayer].currentItem.duration;
    
    CGFloat presentFloatTim = (CGFloat)presentTime.value/presentTime.timescale;
    
    CGFloat totalFloatTim = (CGFloat)totalTime.value / totalTime.timescale;
    
    self.slider.value = presentFloatTim / totalFloatTim;
    
    NSDate *presentDate = [NSDate dateWithTimeIntervalSince1970:presentFloatTim];
    NSDate *totalDate = [NSDate dateWithTimeIntervalSince1970:totalFloatTim];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    if (totalFloatTim / 3600 >= 1) {
        [formatter setDateFormat:@"HH:mm:ss"];
    }else {
        [formatter setDateFormat:@"m:ss"];
    }
    
    NSString *presentStrTime = [formatter stringFromDate:presentDate];
    NSString *totalStrTime = [formatter stringFromDate:totalDate];

    self.leftLable.text = presentStrTime;
    self.rightLable.text = totalStrTime;
}
- (void)xunHuanStyleButton {
//    Image-single random listxunhuan
    switch (_playWay) {
        case singlePlay: {
            [_xunHuanButton setBackgroundImage:[[UIImage imageNamed:@"Image-random"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            _playWay = randomPlay;
        }
            break;
        case randomPlay: {
            [_xunHuanButton setBackgroundImage:[[UIImage imageNamed:@"Image-listxunhuan"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            _playWay = listPlay;
        }
            break;
        case listPlay: {
            [_xunHuanButton setBackgroundImage:[[UIImage imageNamed:@"Image-single"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            _playWay = singlePlay;

        }
            break;
        default:
            break;
    }
    
}
- (void)playViewEditAction {
    
    if (self.isSongList == NO) {
        self.songListView = [[UIView alloc] initWithFrame:CGRectMake(0, self.playViewHeadImageView.frame.origin.y , wide, high / 2 + 40)];

        [self tableViewBuild];
        self.songListView.backgroundColor = [UIColor clearColor];
        self.songListView.alpha = 0.8;
        [self.view addSubview:self.songListView];
        self.songListView.backgroundColor = [UIColor colorWithWhite:0.498 alpha:1.000];
        [_songListView release];
        self.isSongList = YES;
    } else {
        
        [self.songListView removeFromSuperview];
        self.isSongList = NO;
    }
    
}
- (void)tableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:self.songListView.bounds style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[XCUnderSingerTableViewCell class] forCellReuseIdentifier:@"XCUnderSingerTableViewCell"];
    [self.songListView addSubview:self.tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mutableArraySongName.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XCUnderSingerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCUnderSingerTableViewCell"];
    cell.numberLable.text = [NSString stringWithFormat: @"%ld", indexPath.row + 1];
    cell.numberLable.font = [UIFont systemFontOfSize:17];
    cell.nameLable.text = self.nameArray[indexPath.row];
    cell.nameLable.font = [UIFont systemFontOfSize:13];
    cell.albumNameLable.text = self.muatbleArraySingerName[indexPath.row];
    cell.albumNameLable.font = [UIFont systemFontOfSize:12];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 6;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    
    [OnlineAVplay shareAVPlayer].isPlay = YES;
    [[OnlineAVplay shareAVPlayer] playWithUrl:self.mutableArraySongName[indexPath.row]];
    [[OnlineAVplay shareAVPlayer] play];
    self.selectIndex = indexPath.row;
    [self.backGroundimageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    [self.playViewHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    [[XCSingleBottumView shareBottomViewBuild].headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[self.selectIndex]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    self.lableSingerName.text = self.muatbleArraySingerName[_selectIndex];
    self.lableSongName.text = self.nameArray[_selectIndex];

}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
    
}


@end
